package com.iotechn.unimall.app.api.inspection;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dobbinsoft.fw.core.exception.AdminServiceException;
import com.dobbinsoft.fw.core.exception.ServiceException;
import com.dobbinsoft.fw.support.model.Page;
import com.iotechn.unimall.data.domain.ComputerRoomDO;
import com.iotechn.unimall.data.domain.EquipmentDO;
import com.iotechn.unimall.data.domain.InspectionDO;
import com.iotechn.unimall.data.domain.InspectionRoundsDO;
import com.iotechn.unimall.data.enums.ComputerRoomStatusType;
import com.iotechn.unimall.data.enums.InspectionStatusType;
import com.iotechn.unimall.data.exception.ExceptionDefinition;
import com.iotechn.unimall.data.mapper.ComputerRoomMapper;
import com.iotechn.unimall.data.mapper.EquipmentMapper;
import com.iotechn.unimall.data.mapper.InspectionMapper;
import com.iotechn.unimall.data.mapper.InspectionRoundsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


/**
 * Generate Code By Unimall
 */
@Service
public class AppInspectionRoundsServiceImpl implements AppInspectionRoundsService {

    @Autowired
    private InspectionRoundsMapper inspectionRoundsMapper;
    @Autowired
    private ComputerRoomMapper computerRoomMapper;
    @Autowired
    private EquipmentMapper equipmentMapper;
    @Autowired
    private InspectionMapper inspectionMapper;


    @Override
    public boolean delete(Long id, Long adminId) throws ServiceException {
        return inspectionRoundsMapper.deleteById(id) > 0;
    }

    /**
     * 每10分钟执行一次
     */
    @Transactional(rollbackFor = Exception.class)
    @Scheduled(cron = "0 0/10 * * * ?")
    public void rushTime()  throws ServiceException {
        //获取当前年月日
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String date = f.format(new Date(System.currentTimeMillis()));
        //获取当前日期的前两个小时
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.HOUR_OF_DAY, -2);
        Date date1 = calendar.getTime();
        Date now = new Date();
        //查询未结束的巡检轮次
        List<InspectionRoundsDO> inspectionRoundsDOS = inspectionRoundsMapper.selectList(new QueryWrapper<InspectionRoundsDO>()
                .like("inspection_start", date).orderByDesc("rounds"));
        if (inspectionRoundsDOS.size() != 0) {
            InspectionRoundsDO inspectionRoundsDO = inspectionRoundsDOS.get(0);
            //巡检开始时间与当前时间的前两个小时比较
            int result = inspectionRoundsDO.getInspectionStart().compareTo(date1);
            if (result < 0) {
                if (inspectionRoundsDO.getInspectionEnd() == null) {
                    inspectionRoundsDO.setInspectionEnd(now);
                    inspectionRoundsDO.setGmtUpdate(now);
                    inspectionRoundsMapper.updateById(inspectionRoundsDO);
                    //查询机房
                    List<ComputerRoomDO> computerRoomDOS = computerRoomMapper.selectList(new QueryWrapper<>());
                    if (!CollectionUtils.isEmpty(computerRoomDOS)) {
                        for (ComputerRoomDO computerRoomDO : computerRoomDOS) {
                            if ("3".equals(computerRoomDO.getAppStatus())) {
                                //查询该机房下所有设备
                                List<EquipmentDO> equipmentDOS = equipmentMapper.selectList(new QueryWrapper<EquipmentDO>().eq("room_id", computerRoomDO.getId()));
                                if (!CollectionUtils.isEmpty(equipmentDOS)) {
                                    throw new AdminServiceException(ExceptionDefinition.INSPECTION_NOTEND);
//                                    for (EquipmentDO equipmentDO : equipmentDOS) {
//                                        //新增巡检结果
//                                        InspectionDO insertDO = new InspectionDO();
//                                        insertDO.setRoundsId(inspectionRoundsDO.getId());
//                                        insertDO.setRoomName(computerRoomDO.getName());
//                                        insertDO.setRoomId(computerRoomDO.getId());
//                                        insertDO.setEquipmentType(equipmentDO.getEquipmentType());
//                                        insertDO.setEquipmentNo(equipmentDO.getEquipmentNo());
//                                        insertDO.setEquipmentId(equipmentDO.getId());
//                                        insertDO.setInspectionStatus(InspectionStatusType.inspection_missed.getCode());
//                                        inspectionMapper.insert(insertDO);
//                                    }
                                }
                            }
                            //更改机房app状态
                            computerRoomDO.setAppStatus(0);
                            computerRoomMapper.updateById(computerRoomDO);
                        }
                    }

                }
            }
        }
    }

    @Override
    public List<InspectionRoundsDO> appRounds() throws ServiceException {
        //获取当前年月日
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String date = f.format(new Date(System.currentTimeMillis()));
        List<InspectionRoundsDO> inspectionRoundsDOS = inspectionRoundsMapper.selectList(new QueryWrapper<InspectionRoundsDO>()
                .like("inspection_start", date).orderByDesc("rounds"));
        return inspectionRoundsDOS;
    }

    @Override
    public List<InspectionRoundsDO> appRecordRounds(String date) throws ServiceException {
        List<InspectionRoundsDO> inspectionRoundsDOS = inspectionRoundsMapper.selectList(new QueryWrapper<InspectionRoundsDO>()
                .like("inspection_start", date).like("inspection_end", date).orderByAsc("rounds"));
        return inspectionRoundsDOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public InspectionRoundsDO create(String inspectionPeople, Integer rounds) throws ServiceException {
        Date now = new Date();
        InspectionRoundsDO insertDO = new InspectionRoundsDO();
        if("undefined".equals(inspectionPeople) || inspectionPeople == null){
            throw new AdminServiceException(ExceptionDefinition.ACCUN_NOTEND);
        }
        insertDO.setInspectionPeople(inspectionPeople);
        insertDO.setRounds(rounds);
        insertDO.setInspectionStart(now);
        insertDO.setGmtUpdate(now);
        insertDO.setGmtCreate(now);
        //更改机房app状态
        //查询机房
        List<ComputerRoomDO> computerRoomDOS = computerRoomMapper.selectList(new QueryWrapper<>());
        if (!CollectionUtils.isEmpty(computerRoomDOS)) {
            for (ComputerRoomDO computerRoomDO : computerRoomDOS) {
                computerRoomDO.setAppStatus(ComputerRoomStatusType.nostart.getCode());
                computerRoomMapper.updateById(computerRoomDO);
            }
        }
        if (inspectionRoundsMapper.insert(insertDO) > 0) {
            return insertDO;
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String end(Long id) throws ServiceException {
        Date now = new Date();
        InspectionRoundsDO updateDO = inspectionRoundsMapper.selectById(id);
        updateDO.setInspectionEnd(now);
        updateDO.setGmtUpdate(now);
        //更改机房app状态
        //查询机房
        List<ComputerRoomDO> computerRoomDOS = computerRoomMapper.selectList(new QueryWrapper<>());
        if (!CollectionUtils.isEmpty(computerRoomDOS)) {
            for (ComputerRoomDO computerRoomDO : computerRoomDOS) {
                computerRoomDO.setAppStatus(0);
                computerRoomMapper.updateById(computerRoomDO);
            }
        }
        if (inspectionRoundsMapper.updateById(updateDO) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

}
